package com.hs.live.user.impl;

import com.alibaba.nacos.shaded.com.google.common.base.Strings;
import com.hs.live.common.enums.ErrorCodeResp;
import com.hs.live.user.dto.CheckSmsDTO;
import com.hs.live.user.dto.MobileLoginUserDTO;
import com.hs.live.user.dto.UserDTO;
import com.hs.live.user.inter.UserRpcService;
import com.hs.live.user.req.MobileLoginUserReq;
import com.hs.live.user.service.LoginService;
import com.hs.live.user.service.SmsService;
import com.hs.live.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: User 服务提供方 专门对外提供访问接口
 * @Author 胡尚
 * @Date: 2024/9/6 10:02
 */
@Slf4j
@DubboService
public class UserRpcServiceImpl implements UserRpcService {

    @Autowired
    UserService userService;

    @Autowired
    SmsService smsService;

    @Autowired
    LoginService loginService;

    @Override
    public UserDTO getUserById(Long id) {

        // 参数校验
        if (id == null || id <= 0){
            return null;
        }

        return userService.getUserById(id);
    }

    @Override
    public boolean sendSms(String phone) {
        // 参数校验
        if (Strings.isNullOrEmpty(phone)){
            return false;
        }
        return smsService.sendSms(phone);
    }

    @Override
    public CheckSmsDTO checkSms(MobileLoginUserReq req) {
        // 参数校验
        if (Strings.isNullOrEmpty(req.getCode()) || Strings.isNullOrEmpty(req.getMobile())){
            return new CheckSmsDTO(false, ErrorCodeResp.REQ_PARAMETER_UNQUALIFIED.getCode());
        }
        // 验证短信验证码是否正确
        return smsService.checkSms(req);
    }

    @Override
    public MobileLoginUserDTO getOrCreateUserToken(String mobile) {
        // 校验请求参数
        if (Strings.isNullOrEmpty(mobile)){
            // 正常情况下是抛一个业务异常，异常中指定错误code
            log.error("通过手机号进行登录 获取用户token接口，请求参数为null");
            return null;
        }


        return loginService.loginByMobile(mobile);
    }
}
